Macromedia Flex Article

Learning Flex Basics (Part 4): Working with Data Models


Robert Crooks

Robert Crooks

Macromedia Training

Table of Contents

  1. Introduction
  2. Create the Application Object and Application Layout
  3. Add the Form Controls and the DataGrid Controls
  4. Add the New Employee Model, Employee, and Location Models
  5. Instantiate the DataController Class, and Create the Employee and DataController Classes
  6. Add the DataController Methods
  7. Code Listing for data.mxml
  8. Code Listings for DataController.as and Employee.as
  9. Code Listings for locations.xml and employee.xml
  10. Where to Go From Here

In this tutorial, you work with data models both for importing data and storing data entered by users in Macromedia Flex applications. You also learn more about manipulating data in applications by using data entered in a form to create a new object, and by implementing drag and drop between different data grids.

This tutorial is the fourth part in a four-part series written by Robert Crooks of the Macromedia Customer Training group:

If you prefer, you can download the entire set of tutorials.

If you like what you see and want to get more experience with Flex, Macromedia Customer Training offers Fast Track to Macromedia Flex, a two-day onsite training course to jump-start Flex development in your organization. Now let's get started!

As the final tutorial in this series, the aim is partly to familiarize you with additional elements of the Flex class library, and with some additional techniques such as implementing drag and drop interfaces. This tutorial also intends to make you more aware of how you can use Flex to separate out and loosen coupling between the view, data model, and controller portions of an application.

The controller methods know nothing about the details of the view, for instance. The method that adds a new employee takes the data from a model, and so doesn't need to know how you go about gathering the data to put there. Similarly, the methods that handle the drag-and-drop operations work from methods and properties of the event objects that Flex generates and the built-in DragManager class, so they need not know where the data objects are dragged from and dropped—in fact, all you need to do is add the dragEnabled="true" attribute to the target data grids to enable dragging items from one to the other; the controller methods will work without modification.

What You Will Learn

  1. How to use the Application tag.
  2. How to use a Panel container.
  3. How to use the HBox.
  4. How to use the VBox.
  5. How to use the Form container.
  6. How to use TextInput controls.
  7. How to use Button controls.
  8. How to use DataGrid controls.
  9. How to use the Model class for importing data or gathering user input.
  10. How to enable drag and drop for items in a DataGrid.
  11. How to create event handlers for drag and drop operations in ActionScript.

Requirements

To complete this tutorial you will need to install the following software and files:

Macromedia Flex

(Installed and running on any server and platform supported by Flex. Note that the Flex trial is available on CD from the Macromedia store for $8.99. To learn more about the Flex trial, please read the Flex Frequently Asked Questions.)


Dreamweaver MX 2004 or a text editor that you can use to write XML and ActionScript code (a basic text editor such as Notepad is adequate)

Solution files for the code

Prerequisite knowledge:

Building the Application

The application view consists of three panels. One contains a DataGrid component that displays names and locations for all employees, and also provides a simple form for adding a new employee. The other two panels show DataGrids for the Engineering Group and Support Group. You implement drag and drop so that users can drag employees from the general employee grid and add them to either the engineering group or support group grid.

You use data models to import data for existing employees and office locations from XML files. A third model captures data from the employee entry form to add to the employee grid.

You create two associated ActionScript files. The DataController class contains methods to handle events in the application. The controller uses a separate Employee class as a factory to generate new employee data objects.

The finished application

(+) View Larger

Figure 1. The finished application



About the author

Robert Crooks is the Director of Curriculum Development for Macromedia. He has written several courses on creating internet applications including the beta version of Fast Track to Macromedia Flex.